      SUBROUTINE TOSTD ( VALIN, STRIN, VALOUT, STROUT, IERR )
C*
C*                  *******************************
C*                  *******************************
C*                  **                           **
C*                  **          TOSTD            **
C*                  **                           **
C*                  *******************************
C*                  *******************************
C*
C*     SUBPROGRAM :
C*          TO STANDARD UNITS
C*
C*     AUTHOR :
C*          ART RAGOSTA
C*          MS 207-5
C*          AMES RESEARCH CENTER
C*          MOFFETT FIELD,  CA     94035
C*          (415) 694-5578
C*
C*     PURPOSE :
C*          CONVERTS A VALUE WITH NON-STANDARD UNITS TO THE EQUIVALENT
C*          VALUE WITH STANDARD UNITS AND RETURNS THE STANDARD UNITS.
C*
C*     METHODOLOGY :
C*          PARSES THE INPUT UNITS, REPLACES EACH ONE WITH AN EQUIVALENT
C*          STANDARD UNIT AND A SCALE FACTOR, MULTIPLIES THE SCALE FACTORS
C*          TOGETHER AND EVALUATES THE UNITS STRING.
C*
C*     INPUT ARGUMENTS :
C*          VALIN  - THE VALUE OF THE VARIABLE WITH THE ORIGINAL UNITS
C*          STRIN  - THE STRING CONTAINING THE UNITS OF THE INPUT VALUE
C*
C*     OUTPUT ARGUMENTS :
C*          VALOUT - THE VALUE AFTER CONVERSION TO STANDARD UNITS
C*          STROUT - THE STRING CONTAINING THE STANDARD UNITS
C*          IERR   - 0 = NO ERROR
C*                   1 = ILLEGAL CHARACTERS IN UNITS OR BAD EXPONENT
C*                   2 = UNKNOWN UNIT IN INPUT STRING
C*                   3 = TOO COMPLICATED TO EVALUATE OR UNMATCHED PARENS
C*
C*     INTERNAL WORK AREAS :
C*          WORK - TEMPORARY STRING FOR REPLACEMENT OF NON-STD SYMBOLS
C*          TOP, BOTTOM - ARRAYS TO HOLD THE UNITS EXTRACTED FROM STRIN
C*
C*     COMMON BLOCKS :
C*          NONE
C*
C*     FILE REFERENCES :
C*          NONE
C*
C*     SUBPROGRAM REFERENCES :
C*          LENGTH,   PARSE,   STD,   POLISH,   EVAL,   BUILD,  CAPS
C*
C*     ERROR PROCESSING :
C*          ERRORS PASSED FROM SUBROUTINES
C*
C*     TRANSPORTABILITY LIMITATIONS :
C*          NONE
C*
C*     ASSUMPTIONS AND RESTRICTIONS :
C*          THE INPUT UNITS STRING AND THE RESULTING OUTPUT STRING MUST BE
C*           SHORTER THAN 255 CHARACTERS.
C*
C*     LANGUAGE AND COMPILER :
C*          ANSI FORTRAN 77
C*
C*     VERSION AND DATE :
C*          VERSION I.1      13-SEP-85
C*
C*     CHANGE HISTORY :
C*          13-SEP-85    EFFICIENCY IMPROVED, BETTER UNITS CONVERSIONS
C*           7-FEB-85    INITIAL VERSION
C*
C***********************************************************************
C*
      PARAMETER (WLEN=255)
      CHARACTER *(*) STRIN, STROUT
      CHARACTER *(WLEN) WORK
      CHARACTER *6 TOP(40), BOTTOM(40), TOKE(100)
      DOUBLE PRECISION FACTOR, FACTS(100)
      LOGICAL ERROR
C
      WORK   = STRIN
      CALL CAPS ( WORK )
      ERROR  = .FALSE.
      IERR   = 0
      FAC    = 1.0D0
      L      = LENGTH ( WORK )
C
C --- PASS 1, REPLACE '-' WITH '*'   AND   '**' WITH '^'
C
      J = 0
      I = 1
5     IF (WORK(I:I) .EQ. '-') THEN
         J = J + 1
         WORK(J:J) = '*'
      ELSE IF (WORK(I:I+1) .EQ. '**') THEN
         J = J + 1
         I = I + 1
         WORK(J:J) = '^'
C
C --- ALL OTHER CHARACTERS EXCEPT ' ' GET COPIED
C
      ELSE IF (WORK(I:I) .NE. ' ') THEN
         J = J + 1
         WORK(J:J) = WORK(I:I)
      ENDIF
      I = I + 1
      IF ( I .LE. L )GO TO 5
      WORK(J+1:) = ' '
C
C --- PASS 2, PARSE INTO TOKENS
C
      CALL PARSE ( WORK, J, TOKE, NTOKE, ERROR )
      IF ( ERROR ) THEN
         IERR = 1
         RETURN
      ENDIF
C
C --- PASS 3, REPLACE NON-STANDARD UNITS WITH STANDARD
C
      CALL STD ( FACTS, TOKE, NTOKE, ERROR )
      IF ( ERROR ) THEN
         IERR = 2
         RETURN
      ENDIF
C
C --- PASS 4, CONVERT TO REVERSE POLISH
C
      CALL POLISH ( TOKE, NTOKE, FACTS, ERROR )
      IF ( ERROR ) THEN
         IERR = 3
         RETURN
      ENDIF
C
C --- PASS 5, EVALUATE CONVERSION FACTORS
C
      CALL EVAL ( TOKE, NTOKE, FACTS, TOP, NTOP, BOTTOM, NBOT, FACTOR )
C
      VALOUT = VALIN * SNGL (FACTOR)
C
C --- PASS 6, BUILD OUTPUT UNIT STRING
C
      CALL BUILD ( STROUT, TOP, NTOP, BOTTOM, NBOT )
      RETURN
      END
C
C---END TOSTD
C
